package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.model.Ad;
import com.cskaoyan.model.AdExample;
import com.cskaoyan.model.bo.SelectAdBO;
import com.cskaoyan.model.vo.AdDataVO;
import com.cskaoyan.model.vo.AdQueryAdInfoVO;
import com.cskaoyan.service.AdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: AdServiceImpl
 * @Description: 广告模块接口实现类
 * @Author:YPS
 * @Date: 2022/5/5 20:58
 * @Version: 1.0
 **/
@Transactional
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    AdMapper adMapper;
    @Override
    public AdDataVO queryAd(SelectAdBO adBO) {

        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        adExample.setOrderByClause(adBO.getSort() + " " + adBO.getOrder());
        criteria.andDeletedEqualTo(false);
        if (!(adBO.getName() == null || "".equals(adBO.getName()))){
            criteria.andNameLike("%" + adBO.getName() + "%");
        }
        if (!(adBO.getContent() == null||"".equals(adBO.getContent()))){
            criteria.andContentLike("%" + adBO.getContent() + "%");
        }
        PageHelper.startPage(adBO.getPage(), adBO.getLimit());
        List<Ad> adList = adMapper.selectByExample(adExample);
        PageInfo<Ad> adPageInfo = new PageInfo<>(adList);
        long total = adPageInfo.getTotal();

        List<AdQueryAdInfoVO> adQueryAdInfoVOS = new ArrayList<>();
        for (Ad ad : adList) {
            AdQueryAdInfoVO adQueryAdInfoVO = new AdQueryAdInfoVO();
            adQueryAdInfoVO.setAddTime(ad.getAddTime());
            adQueryAdInfoVO.setContent(ad.getContent());
            adQueryAdInfoVO.setDeleted(ad.getDeleted());
            adQueryAdInfoVO.setEnabled(ad.getEnabled());
            adQueryAdInfoVO.setId(ad.getId());
            adQueryAdInfoVO.setLink(ad.getLink());
            adQueryAdInfoVO.setName(ad.getName());
            adQueryAdInfoVO.setPosition(ad.getPosition());
            adQueryAdInfoVO.setUpdateTime(ad.getUpdateTime());
            adQueryAdInfoVO.setUrl(ad.getUrl());
            adQueryAdInfoVOS.add(adQueryAdInfoVO);
        }
        AdDataVO adDataVO = new AdDataVO();
        adDataVO.setTotal(total);
        adDataVO.setPage(adBO.getPage());
        adDataVO.setPages(adPageInfo.getPages());
        adDataVO.setLimit(adBO.getLimit());
        adDataVO.setList(adQueryAdInfoVOS);
        return adDataVO;
    }

    @Override
    public AdQueryAdInfoVO insertAd(AdQueryAdInfoVO adQueryAdInfoVO) {
        Ad ad = new Ad();

        ad.setContent(adQueryAdInfoVO.getContent());
        ad.setEnabled(adQueryAdInfoVO.getEnabled());
        ad.setLink(adQueryAdInfoVO.getLink());
        ad.setName(adQueryAdInfoVO.getName());
        ad.setPosition(adQueryAdInfoVO.getPosition());
        ad.setUrl(adQueryAdInfoVO.getUrl());
        ad.setDeleted(false);
        Date date = new Date();

        ad.setAddTime(date);
        ad.setUpdateTime(date);
        adMapper.insertAd(ad);
        adQueryAdInfoVO.setId(ad.getId());
        adQueryAdInfoVO.setUpdateTime(date);
        adQueryAdInfoVO.setAddTime(date);
        return adQueryAdInfoVO;
    }

    @Override
    public AdQueryAdInfoVO updateAd(AdQueryAdInfoVO adQueryAdInfoVO) {
        Ad ad = new Ad();
        ad.setId(adQueryAdInfoVO.getId());
        ad.setContent(adQueryAdInfoVO.getContent());
        ad.setEnabled(adQueryAdInfoVO.getEnabled());
        ad.setLink(adQueryAdInfoVO.getLink());
        ad.setName(adQueryAdInfoVO.getName());
        ad.setPosition(adQueryAdInfoVO.getPosition());
        ad.setUrl(adQueryAdInfoVO.getUrl());
        Date date = new Date();
        ad.setUpdateTime(date);
        adMapper.updateByPrimaryKeySelective(ad);
        adQueryAdInfoVO.setUpdateTime(date);
        return adQueryAdInfoVO;
    }

    @Override
    public void deleteAd(AdQueryAdInfoVO adQueryAdInfoVO) {
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        Ad ad = new Ad();
        ad.setId(adQueryAdInfoVO.getId());
        ad.setContent(adQueryAdInfoVO.getContent());
        ad.setEnabled(adQueryAdInfoVO.getEnabled());
        ad.setLink(adQueryAdInfoVO.getLink());
        ad.setName(adQueryAdInfoVO.getName());
        ad.setPosition(adQueryAdInfoVO.getPosition());
        ad.setDeleted(true);
        ad.setUrl(adQueryAdInfoVO.getUrl());
        Date date = new Date();
        ad.setUpdateTime(date);
        adMapper.updateByPrimaryKeySelective(ad);
    }
}
